from rest_framework import serializers
from ..models.Publications import Publication, Book, Journal, Conference
from ..models.Keyword import Keyword
from ..models.Department import Department
from ..models.Institution import Institution
from ..models.Author import Author
from ..serializers.authorListSerializer import AuthorListSerializer
from ..serializers.instInfoSerializer import InstInfoSerializer
from ..serializers.departInfoSerializer import DepInfoSerializer
from ..serializers.keywordListSerializer import SubjectListSerializer

class PublicationCreateSerializer(serializers.ModelSerializer):
    subjects = serializers.PrimaryKeyRelatedField(many=True, queryset=Keyword.objects.all(), required=False)
    institutions = serializers.PrimaryKeyRelatedField(many=True, queryset=Institution.objects.all(), required=False)
    departments = serializers.PrimaryKeyRelatedField(many=True, queryset=Department.objects.all(), required=False)
    
    class Meta:
        model = Publication
        fields = ["name", "zh_name", 'abbreviation', 'website', 'description', 'subjects', 'institutions', 'departments']
        
    def create(self, validated_data):
        subjects_data = validated_data.pop('subjects', [])
        institutions_data = validated_data.pop('institutions', [])
        departments_data = validated_data.pop('departments', [])
        p = Publication.objects.create(**validated_data)

        # 使用 .set() 方法设置多对多关系
        p.subjects.set(subjects_data)
        p.institutions.set(institutions_data)
        p.departments.set(departments_data)

        return p

        
class BookCreateSerializer(serializers.ModelSerializer):
    authors = serializers.PrimaryKeyRelatedField(many=True, queryset=Author.objects.all(), required=False)
    
    class Meta:
        model = Book
        fields = ["publish_date", "isbn", 'page_number', 'language', 'authors']
        
        
class JournalCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Journal
        fields = ["issn", "cas", 'impact_factor', 'founding_datej', 'contact_emailj', 'publication_cycle']
        

class ConferenceCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Conference
        fields = ["contact_emailc", "founding_datec", 'meeting_cycle']
        
        
class PublicationInfoSerializer(serializers.ModelSerializer):
    subjects = serializers.SerializerMethodField()
    institutions = serializers.SerializerMethodField()
    departments = serializers.SerializerMethodField()
    
    class Meta:
        model = Publication
        fields = ["uuid", "name", "zh_name", 'abbreviation', 'website', 'description', 'subjects', 'institutions', 'departments']
        
    def get_institutions(self, obj):
        institution_list = []
        for institution in obj.institutions.all():
            institution_list.append(InstInfoSerializer(institution).data)
        return institution_list
    
    def get_subjects(self, obj):
        subjects_list = []
        for subject in obj.subjects.all():
            subjects_list.append(SubjectListSerializer(subject).data)
        return subjects_list
    
    def get_departments(self, obj):
        department_list = []
        for department in obj.departments.all():
            department_list.append(DepInfoSerializer(department).data)
        return department_list
    
    
class BookInfoSerializer(serializers.ModelSerializer):
    authors = serializers.SerializerMethodField()
    
    class Meta:
        model = Book
        fields = ["publish_date", "isbn", 'page_number', 'language', 'authors']
        
    def get_authors(self, obj):
        author_list = []
        for author in obj.authors.all():
            author_list.append(AuthorListSerializer(author).data)
        return author_list
    

class JournalInfoSerializer(serializers.ModelSerializer):
     class Meta:
        model = Journal
        fields = ["issn", "cas", 'impact_factor', 'founding_datej', 'contact_emailj', 'publication_cycle']
        
        
class ConferenceInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Conference
        fields = ["contact_emailc", "founding_datec", 'meeting_cycle']